home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / programr / appsrcs.zip / APPUN.ZIP / EXEC.C < prev    next >
C/C++ Source or Header  |  1993-02-21  |  3KB  |  126 lines

  1. /*
  2.     exec.c
  3.  
  4.     This module contains the ExecApp and IsAppRunning functions
  5.     It relies on the global ghInstance;
  6.  
  7. */
  8.  
  9. //compile with the strictest error checking
  10. #define STRICT
  11. #include <windows.h>
  12. #include <windowsx.h>
  13. #include "apprun.h"
  14.  
  15. // local data
  16. static char *szProgramsSection = "programs"; // WIN.INI section
  17.  
  18. // local functions
  19. static int TryWinExec(LPSTR pszPath, LPSTR pszParams);
  20.  
  21. //
  22. // Run a named application
  23. //
  24.  
  25. UINT ExecApp(HWND hwndParent, LPSTR pszPath, LPSTR pszParams)
  26. {
  27.     char szPath[_MAX_PATH];
  28.     char szNewPath[_MAX_PATH];
  29.     char szDrive[_MAX_DRIVE];
  30.     char szDir[_MAX_DIR];
  31.     char szFname[_MAX_FNAME];
  32.     char szExt[_MAX_EXT];
  33.     int i;
  34.  
  35.     //
  36.     // Copy the path and convert to upper case
  37.     //
  38.  
  39.     _fstrcpy(szPath, pszPath);
  40.     _fstrupr(szPath);
  41.  
  42.     //
  43.     // Split up the components of the path
  44.     //
  45.  
  46.     _splitpath(szPath, szDrive, szDir, szFname, szExt);
  47.  
  48.     //
  49.     // See if it already has an extension
  50.     // and if not, provide .EXE
  51.     //
  52.  
  53.     if (!_fstrlen(szExt)) {
  54.  
  55.         _fstrcpy(szExt, ".EXE");
  56.  
  57.     } else {
  58.  
  59.         //
  60.         // Make sure it's EXE and not something bogus
  61.         //
  62.  
  63.         if (!_fstricmp(szExt, ".EXE")) {
  64.  
  65.             return INVALID_NAME;
  66.         }
  67.     }
  68.  
  69.     //
  70.     // Try and run it.
  71.     //
  72.  
  73.     _makepath(szNewPath, szDrive, szDir, szFname, szExt);
  74.  
  75.     i = TryWinExec(szNewPath, pszParams);
  76.  
  77.     if ((i == RAN_OK) || (i == SOME_ERROR)) {
  78.         return i;
  79.     }
  80.  
  81.     //
  82.     // It couldn't be found because it either wasn't on the search
  83.     // path or because the path supplied was invalid.
  84.     // See if we have a WIN.INI entry for it.
  85.     //
  86.  
  87.     _makepath(szPath, "", "", szFname, szExt);
  88.     i = GetProfileString(szProgramsSection,    // [programs]
  89.                          szPath,                // <app>.EXE
  90.                          "",                    // default
  91.                          szNewPath,             // result
  92.                          sizeof(szNewPath));
  93.  
  94.     if (i != 0)
  95.     return TryWinExec(szNewPath, pszParams);
  96.     else
  97.     return SOME_ERROR;
  98. }
  99.  
  100. //
  101. // Try running an app by calling WinExec.  If it works, return some
  102. // info about the app in the struct pointed to by pInfo.
  103. // It returns RAN_OK, NOT_FOUND or SOME_ERROR.
  104. //
  105.  
  106. static int TryWinExec(LPSTR pszPath, LPSTR pszParams)
  107. {
  108.     char szCmdLine[_MAX_PATH + 256];
  109.     UINT uiResult;
  110.  
  111.     _fstrcpy(szCmdLine, pszPath);
  112.     _fstrcat(szCmdLine, " ");
  113.     _fstrcat(szCmdLine, pszParams);
  114.     uiResult = WinExec(szCmdLine, SW_SHOWNORMAL);
  115.  
  116.     if ((uiResult == 2) || (uiResult == 3)) {
  117.         return NOT_FOUND;
  118.     }
  119.  
  120.     if (uiResult < 32) {
  121.         return SOME_ERROR;
  122.     }
  123.  
  124.     return RAN_OK;
  125. }
  126.